package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;


//这是WebMvc的配置类
@Configuration
public class ReggieWebMvcConfig implements WebMvcConfigurer {
	@Autowired
	//自动注入登录拦截器
	private LoginCheckInterceptor loginCheckInterceptor;
	
	//设置拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
	//定义放行路径
		ArrayList<String> urls = new ArrayList<>();
		
		urls.add("/front/**");//app系统静态资源访问路径
		urls.add("/error");//错误的请求
		urls.add("/user/sendMsg");//app验证码发送
		urls.add("/user/login"); //app登录
		urls.add("/user/logout");//app退出
		
		registry.addInterceptor(loginCheckInterceptor)
				.addPathPatterns("/**")//拦截所有
				.excludePathPatterns(urls);//放行的路径
		
	}
	
	//静态资源映射
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//这个待了解;
		registry.addResourceHandler("/front/**")
				.addResourceLocations("classpath:/front/");
	
	}
	
	//扩展mvc框架的消息转换器
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		//创建消息转换器对象
		MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
		//底层使用jackson将java对象转换为json
		messageConverter.setObjectMapper(new JacksonObjectMapper());
		
		//将消息转换对象追加到mvc框架的转换器集合中
		converters.add(0,messageConverter);
	}
}
